﻿/**********************************************************************************
 * Description: Fjaspx!NT V2.0    
 * MemberVariable:
 * Functions:       
 * Modify information:     
 * 2009-8-5  New 杜耀辉  Create Class 
 * 
 * ********************************************************************************/
using System;
using System.Data;
using System.Web.UI.WebControls;
using Fjaspx.Common;
using System.IO;

namespace Fjaspx.UI.Web
{
    public partial class Administrator_Product_add : AdminPage
    {

        #region ClassKind属性
        protected int ClassKind
        {
            get
            {
                return (int)ViewState["ClassKind"];
            }
            set
            {
                ViewState["ClassKind"] = value;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                SystemCms.AdminSesion();

                this.ClassKind = Convert.ToInt32(Request.QueryString["ClassKind"] == null ? "200" : Request.QueryString["ClassKind"]);
                //绑定分类
                DdlMenu_bind();

                txtauthor.Text = Fjaspx.Common.Session.Get("user");
            }
        }
        protected void ImageButton_add_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            string pic = Fjaspx.Common.input.Htmls(this.txtpic.Text);


            string title = Fjaspx.Common.input.Htmls(this.txttitle.Text);

            if (this.styles.Value != "")
            {
                title = "<font size='" + this.styles.Value + "'>" + this.txttitle.Text + "</font>";
            }
            if (this.colors.Value != "")
            {
                title = "<font color='" + colors.Value + "'>" + this.txttitle.Text + "</font>";
            }
            if (this.colors.Value != "" && this.styles.Value != "")
            {
                title = "<font size='" + this.styles.Value + "' color='" + colors.Value + "'>" + this.txttitle.Text + "</font>";
            }

            string keyword = Fjaspx.Common.input.Htmls(this.keyword.Text);//默认值
            string zhaiyao = Fjaspx.Common.input.Htmls(this.txtzhaiyao.Text);
            int classname = int.Parse(this.DdlMenu.SelectedValue);
            string contents = Fjaspx.Common.input.Htmls(FCKeditor1.Value);
            string infor = Fjaspx.Common.input.Htmls(this.txtinfor.Text);

            string author = Fjaspx.Common.input.Htmls(this.txtauthor.Text);
            string url = Fjaspx.Common.input.Htmls(this.txturl.Text);
            string tag = Fjaspx.Common.input.Htmls(this.txttag.Text);
            int hits = int.Parse(this.txthits.Text.Trim());
            int dig = int.Parse(this.txtdig.Text.Trim());

            string jumpurl = "";
            if (this.jumpurl_on_of.Checked)
            {
                jumpurl = Fjaspx.Common.input.Htmls(this.jumpurl.Text.Trim());
            }
            int orders = 0;
            if (this.orders.Value != "")
            {
                orders = int.Parse(this.orders.Value);
            }

            int ding = 1;
            if (this.ding.Checked)
            {
                ding = 0;
            }
            int tuijian = 1;
            if (this.tuijian.Checked)
            {
                tuijian = 0;
            }
            int switchs = 1;
            if (this.switchs.Checked)
            {
                switchs = 0;
            }


            Fjaspx.Sys.Model.ROYcms_news model = new Fjaspx.Sys.Model.ROYcms_news();
            model.pic = pic;
            model.title = title;
            if (keyword == "") { keyword = title; }
            else { model.keyword = keyword; }
            if (zhaiyao == "") { zhaiyao = title; }
            else { model.zhaiyao = zhaiyao; }
            model.classname = classname;
            model.contents = contents;
            model.infor = infor;
            model.jumpurl = jumpurl;
            model.author = author;
            model.url = url;
            model.tag = tag;
            model.orders = orders;
            model.ding = ding;
            model.tuijian = tuijian;
            model.dig = dig;
            model.hits = hits;
            model.switchs = switchs;
            model.link = "";
            model.type = "Product";

            Fjaspx.Sys.Bll.ROYcms_news bll = new Fjaspx.Sys.Bll.ROYcms_news();
            bll.Add(model);
            Response.Redirect("/administrator/Message.aspx?message=录入新闻--<b>[" + title + "]</b>--成功！&z=yes");
        }

        #region 数据绑定
        void DdlMenu_bind()
        {
            DataSet ds = new Fjaspx.Sys.Bll.ROYcms_class().GetClassList(200);

            this.DdlMenu.Items.Clear();
            this.DdlMenu.Items.Add(new ListItem("请选择所属分类", ""));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int ClassTj = Convert.ToInt32(dr["ClassTj"]);
                string Id = dr["Id"].ToString().Trim();
                string ClassName = dr["ClassName"].ToString().Trim();

                if (ClassTj == 1)
                {
                    this.DdlMenu.Items.Add(new ListItem(ClassName, Id));

                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = Fjaspx.Common.StringPlus.StringOfChar(ClassTj - 1, "　") + ClassName;

                    this.DdlMenu.Items.Add(new System.Web.UI.WebControls.ListItem(ClassName, Id));
                }
            }

        }
        #endregion


        protected void Button_add_img_Click(object sender, EventArgs e)
        {
            AddImg(Fjaspx.Common.Session.Get("user")+"/");
        }



        void AddImg(string path)
        {
            UP_img up_img = new UP_img();
            if (FileUpload1.HasFile)
            {
                string fileContentType = FileUpload1.PostedFile.ContentType;
                if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
                {
                    string name = FileUpload1.PostedFile.FileName;                         // 客户端文件路径
                    FileInfo file = new FileInfo(name);
                    string fileName = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + name.Substring(name.LastIndexOf("."), name.Length - name.LastIndexOf("."));                                           // 文件名称
                    //string fileName_s = "x_" + file.Name;                                  // 缩略图文件名称
                    //string fileName_sy = "text_" + file.Name;                              // 水印图文件名称（文字）
                    //string fileName_syp = "w_" + file.Name;                            // 水印图文件名称（图片）
                    string webFilePath = Server.MapPath("~/App_Product/"+ path);          // 服务器端文件路径

                    string webFilePath_s = Server.MapPath("~/App_Product/" + path+"s_");　　  // 服务器端缩略图路径
                        //string webFilePath_sy = Server.MapPath("ImgUpload/" + fileName_sy);　  // 服务器端带水印图路径(文字)
                        //string webFilePath_syp = Server.MapPath("../ImgUpload/W_" + fileName);　// 服务器端带水印图路径(图片)
                        //string webFilePath_sypf = Server.MapPath("../ImgUpload/water.png");　               // 服务器端水印图路径(图片)

                    if (!Directory.Exists(webFilePath))//判断是否存在
                    {
                        Directory.CreateDirectory(webFilePath);//创建新路径
                    }

                    if (Directory.Exists(webFilePath))//判断是否存在
                    {
                        try
                        {
                            FileUpload1.SaveAs(webFilePath + fileName);                                   // 使用 SaveAs 方法保存文件
                            //AddWater(webFilePath, webFilePath_sy);
                            //up_img.AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);
                            string[] Size = DropDownList_ImgSize.SelectedValue.Split(',');
                            up_img.MakeThumbnail(webFilePath + fileName, webFilePath_s + fileName, Convert.ToInt32(Size[0]), Convert.ToInt32(Size[1]), "W");          // 生成缩略图方法
                            Response.Write("上传的图片<br><img src=../../App_Product/" + path + fileName + " />");
                            txtpic.Text = fileName;
                            //System.IO.File.Delete(webFilePath);
                        }
                        catch 
                        {
                            Response.Write("文件上传失败!");
                        }
                    } 
 
                }
                else
                {
                    Response.Write("文件类型不符!");
                }
            }
        }



        //   #region 判断是否是最底层可选类型

        //public bool IfClass() {

        //       if (BLL.GetSubClassList(BLL.GetClassId(int.Parse(this.DdlMenu.SelectedValue))) == null) 
        //       {
        //       return true;
        //       }
        //       else
        //       {
        //       return false;
        //       }
        //   }
    }
}